package cn.jsony.basic.controller;

import cn.jsony.basic.config.BusinessException;
import cn.jsony.basic.domain.SmsCodeDto;
import cn.jsony.basic.dto.SmsDto;
import cn.jsony.basic.service.IVerifyCodeService;
import cn.jsony.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService iVerifyCodeService;
    @GetMapping("/img/{key}")
    public AjaxResult imgVerifyCode(@PathVariable("key")String key){
        try {
            String base64Str = iVerifyCodeService.getImgCode(key);
            return AjaxResult.me().setResultObj(base64Str);
        }catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,"获取图片验证码失败");
        }
        catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙,请稍后再试");
        }
    }


    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody SmsCodeDto smsCodeDto){
        try {
            iVerifyCodeService.smsCode(smsCodeDto);
            return new AjaxResult();
        }catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙,请稍后再试");
        }
    }

    @PostMapping("/binderSmsCode")
    public AjaxResult binderSmsCode(@RequestBody SmsDto smsDto){
        try {
            iVerifyCodeService.binderSmsCode(smsDto);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }
    }
}
